//根据传入的分类从数据库中获取对应的文章
const redis = require('../../../../redis/init')
const  getCateGoryArticle = async (req,res) => {
    const {id} = req.query;
    const isExist = await redis.EXISTS(`articlesList`)
    if(isExist){
        const articles = await redis.lRange("articlesList", 0, -1);     
        let categoryArticles = []
       articles.forEach(article =>{
            const articleObj = JSON.parse(article)
            if(articleObj.category_id == id){
                categoryArticles.push(articleObj)
            }
        })
        res.send({
            code:200,
            total:categoryArticles.length,
            categoryArticles
        })
    }
    else{
        //查询mysql数据库返回数据
        const {articlesModule} = require('../../../../../ORM/public/Model')
        const categoryArticles = await articlesModule.findAll({
            where:{
                category_id:id
            }
        })
        res.send({
            code:200,
            total:categoryArticles.length,
            categoryArticles
        })
    }
}
module.exports = getCateGoryArticle